<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构名称" prop="tenantName">
        <el-input v-model="queryParams.tenantName" placeholder="请输入机构名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="区域" prop="region">
        <el-cascader ref="areaPicker" v-model="region" :props="areaProps" :show-all-levels="true"
          @change="handleChangeArea" style="width: 100%;" clearable></el-cascader>
      </el-form-item>
      <!-- TODO 字典获取、多选 内容：产权单位、物业 -->
<!--      <el-form-item label="类型" prop="type">-->
<!--        <el-select v-model="queryParams.type" placeholder="请选择" clearable>-->
<!--          <el-option v-for="dict in dict.type.department_type" :key="dict.value" :label="dict.label"-->
<!--            :value="dict.value" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['sys-enterprise:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:rule:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:rule:remove']"
          >删除</el-button
        >
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" ref="table">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="机构名称" align="center" prop="tenantName" />
      <el-table-column label="区域" align="center" prop="area">
        <template slot-scope="scope">
          <span>{{ scope.row.provinceName }}{{ scope.row.cityName }}{{ scope.row.districtName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="tenantStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.tenantStatus === '1'" type="success">正常</el-tag>
          <el-tag v-if="scope.row.tenantStatus === '0'" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="类型" align="center" prop="type">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.department_type" :value="item" v-for="(item, index) in scope.row.typeList"-->
<!--            :key="index" />-->
<!--        </template>-->
<!--      </el-table-column>-->
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- TODO 添加按钮权限 -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['sys-enterprise:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAccount(scope.row)"
            v-hasPermi="['sys-enterprise:account']">账号</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAuth(scope.row)"
            v-hasPermi="['sys-enterprise:auth']">企业权限</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['sys-enterprise:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 新增/编辑 -->
    <edit v-model="open" :title="title" :data="form" @success="getList" />
    <!-- 账户弹窗 -->
    <account v-model="accountVisible" :tenantId="tenantId" />
    <!-- 权限弹窗 -->
    <auth v-model="authVisible" :tenantId="tenantId" />
  </div>
</template>

<script>
import { pageDepartment, getDepartment, deleteDepartment } from "@/api/sys-enterprise";
import { listArea } from "@/api/system/config";
import edit from "./components/edit.vue";
import account from './components/account/index.vue'
import Auth from './components/auth.vue';

export default {
  name: "enterprise-list",
  dicts: [
    "department_type",
  ],
  components: {
    edit,
    account,
    Auth
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      //退订规则详情数据
      unsubscribeRuleData: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //详情弹窗
      detailVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantName: undefined,
        provinceId: undefined,
        cityId: undefined,
        districtId: undefined,
        type: undefined,
      },
      // 表单参数
      form: {},
      id: undefined,
      //账号弹窗
      accountVisible: false,
      tenantId:undefined,
      //权限弹窗
      authVisible: false,
      region:undefined,
      areaProps: {
        lazy: true,
        // emitPath: false,
        lazyLoad(node, resolve) {
          listArea({ level: node.level + 1, parentId: node.value }).then(
            (res) => {
              const areas = res.data.map((value, i) => ({
                value: value.id,
                label: value.name,
                leaf: node.level >= 2,
              }));
              resolve(areas);
            }
          );
        },
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      pageDepartment(this.queryParams)
        .then((response) => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.region = undefined
      this.queryParams.provinceId = undefined
      this.queryParams.cityId = undefined
      this.queryParams.districtId = undefined
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.unsubscribeRuleId);
      this.single = selection.length != 1;
      console.log("single", this.single);
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.open = true;
      this.title = "新增机构信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.tenantId || this.ids;
      getDepartment(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改规则";
      });
    },
    //账号操作
    handleAccount(row) {
      this.tenantId = row.tenantId
      this.accountVisible = true
    },
    //企业权限
    handleAuth(row) {
      this.tenantId = row.tenantId
      this.authVisible = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.tenantId || this.ids;
      this.$modal
        .confirm(
          '确定删除数据？'
        )
        .then(function () {
          return deleteDepartment(Ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/post/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
    //请空选择项
    clearSelect() {
      this.$refs.table.clearSelection();
    },
    //区域选择
    handleChangeArea(val) {
      const checkedNode = this.$refs["areaPicker"].panel.getNodeByValue(val);
      const ids = checkedNode.path;
      this.queryParams.provinceId = ids[0]
      this.queryParams.cityId = ids[1]
      this.queryParams.districtId = ids[2]
    },
  },
};
</script>
<style lang="scss" scoped>
.info {
  padding: 10px 15px;
  margin: 15px 0;
  background: #e6f7ff;
  color: #1890ff;

  span {
    color: #565c5f;
  }

  .clear {
    margin-left: 20px;
    cursor: pointer;
  }
}
</style>
